/*
 * StringEvaluationSource.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.interpreter;

import org.scriptme.util.StringTokenizer;

/**
 * Describe a string used as a source. The string is trimmed to its first
 * significant characters during display.
 */
public class StringEvaluationSource extends EvaluationSource {

	/** The string. */
	private String theString;

	/**
	 * Create a string source description.
	 * 
	 * @param theString
	 *            Describe the source
	 * @param previousSource
	 *            Describe the calling source
	 */
	public StringEvaluationSource(String theString,
			EvaluationSource previousSource) {
		super(previousSource);
		this.theString = theString;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.interpreter.EvaluationSource#getEvaluationSourceText()
	 */
	protected String getEvaluationSourceText() {
		String displayString = new String("");
		// All this to print just first line of string...
		boolean firstLineFound = false;
		boolean moreLinesFound = false;
		StringTokenizer t = new StringTokenizer(theString, "\n\r");
		while (t.hasMoreTokens()) {
			String theLine = t.nextToken();
			if (theLine.equals("\n") || theLine.equals("\r"))
				continue;
			if (theLine.trim().length() > 0) { // Skip any leading empty lines
				if (!firstLineFound) {
					displayString = theLine;
					firstLineFound = true;
					continue;
				}
				moreLinesFound = true;
				break;
			}
		}
		if (moreLinesFound) {
			return "in string starting with: '" + displayString + "'...";
		} else {
			return "in string: '" + displayString + "'";
		}
	}
}
